﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Diagnostics;
using System.ServiceModel;
using System.Runtime.Serialization;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;


namespace DragDraw.Service
{
    public class FriendRelationData
    {
        ///<summary>
        ///插入朋友关系信息
        ///</summary>
        ///<exception cref="DataErrorException">数据库执行错误</exception>
        ///<exception cref="DataNotFoundException">查找不到相应记录</exception>
        static public bool Insert(string userID1,string userID2)//what if the id exist?
        {
            SqlCommand curCommand;
            curCommand = new SqlCommand();
            curCommand.CommandType = CommandType.StoredProcedure;
            curCommand.CommandText = "InsertFriendRelation";
            curCommand.Parameters.Add("@result", SqlDbType.Int);
            curCommand.Parameters.Add("@userID1", SqlDbType.NVarChar, 20);
            curCommand.Parameters.Add("@userID2", SqlDbType.NVarChar, 20);
            curCommand.Parameters["@result"].Direction = ParameterDirection.Output;
            curCommand.Parameters["@userID1"].Value = userID1;
            curCommand.Parameters["@userID2"].Value = userID2;

            curCommand.Connection = SqlDataManager.OpenConnection();
            int result = 0;
            try
            {
                result = curCommand.ExecuteNonQuery();
            }
            catch (Exception error)
            {
                Console.WriteLine(error.Message);
                throw new DataErrorException("FriendRelationData.Insert:执行SqlCommand的时候发生错误！", error);
            }
            finally
            {
                SqlDataManager.CloseConnection(curCommand.Connection);
            }
            if (Convert.ToInt32(curCommand.Parameters["@result"].Value) == 0)//有没有执行sql语句？
            {
                throw new DataInsertException("FriendRelationData.Insert:没有执行sql语句！", null);
            }
            if (result <= 0)//执行的sql语句是否成功？
            {
                throw new DataErrorException("FriendRelationData.Insert:执行了sql语句，可是sql语句没有影响任何一条记录！", null);
            }
            return true;
        }
        ///<summary>
        ///删除朋友关系信息
        ///</summary>
        ///<exception cref="DataErrorException">数据库执行错误</exception>
        ///<exception cref="DataNotFoundException">查找不到相应记录</exception>
        static public bool Delete(string userID1,string userID2)
        {
            SqlCommand curCommand;
            curCommand = new SqlCommand();
            curCommand.CommandType = CommandType.StoredProcedure;
            curCommand.CommandText = "DeleteFriendRelation";
            curCommand.Parameters.Add("@result", SqlDbType.Int);
            curCommand.Parameters.Add("@userID1", SqlDbType.NVarChar, 20);
            curCommand.Parameters.Add("@userID2", SqlDbType.NVarChar, 20);
            curCommand.Parameters["@result"].Direction = ParameterDirection.Output;
            curCommand.Parameters["@userID1"].Value = userID1;
            curCommand.Parameters["@userID2"].Value = userID2;

            curCommand.Connection = SqlDataManager.OpenConnection();
            int result = 0;
            try
            {
                result = curCommand.ExecuteNonQuery();
            }
            catch (Exception error)
            {
                Console.WriteLine(error.Message);
                throw new DataErrorException("FriendRelationData.Delete:执行SqlCommand的时候发生错误！", error);
            }
            finally
            {
                SqlDataManager.CloseConnection(curCommand.Connection);
            }
            if (Convert.ToInt32(curCommand.Parameters["@result"].Value) == 0)//有没有执行sql语句？
            {
                throw new DataErrorException("FriendRelationData.Delete:没有执行sql语句！", null);
            }
            if (result <= 0)//执行的sql语句是否成功？
            {
                throw new DataErrorException("FriendRelationData.Delete:执行了sql语句，可是sql语句没有影响任何一条记录！", null);
            }
            return true;
        }
        ///<summary>
        ///查询朋友关系信息
        ///</summary>
        ///<exception cref="DataErrorException">数据库执行错误</exception>
        ///<exception cref="DataNotFoundException">查找不到相应记录</exception>
        static public List<string> QueryList(string userID)//if does not exist,return null
        {
            List<string> result_stringList = new List<string>();
            SqlCommand curCommand;
            curCommand = new SqlCommand();
            curCommand.CommandType = CommandType.Text;

            curCommand.CommandText = "select UserID2 from FriendRelation where UserID1='" + userID + "'";
            curCommand.Connection = SqlDataManager.OpenConnection();
            try
            {
                SqlDataReader reader = curCommand.ExecuteReader(CommandBehavior.Default);
                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        result_stringList.Add(reader.GetString(0));
                    }
                }
            }
            catch (Exception error)
            {
                Console.WriteLine(error.Message);
                throw new DataErrorException("FriendRelationData.QueryList:执行第一个SqlCommand的时候发生异常！", error);
            }
            finally
            {
                SqlDataManager.CloseConnection(curCommand.Connection);
            }
            curCommand.CommandText = "select UserID1 from FriendRelation where UserID2='" + userID + "'";
            curCommand.Connection = SqlDataManager.OpenConnection();
            try
            {
                SqlDataReader reader = curCommand.ExecuteReader(CommandBehavior.Default);
                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        result_stringList.Add(reader.GetString(0));
                    }
                }
            }
            catch (Exception error)
            {
                Console.WriteLine(error.Message);
                return null;
            }
            finally
            {
                SqlDataManager.CloseConnection(curCommand.Connection);
            }
            if (0 == result_stringList.Count)
                throw new DataNotFoundException("FriendRelationData.QueryList:所有语句执行，可是没有找到匹配记录！", null);
            return result_stringList;
        }
        ///<summary>
        ///显示朋友信息
        ///</summary>
        ///<exception cref="DataErrorException">数据库执行错误</exception>
        ///<exception cref="DataNotFoundException">查找不到相应记录</exception>
        static public void Show()
        {
            Console.WriteLine("-----FriendRelation Table-----------------");
            SqlCommand curCommand;
            curCommand = new SqlCommand();
            curCommand.CommandType = CommandType.Text;
            curCommand.CommandText = "select * from FriendRelation";
            curCommand.Connection = SqlDataManager.OpenConnection();
            try
            {
                SqlDataReader reader = curCommand.ExecuteReader(CommandBehavior.Default);
                while (reader.Read())
                {
                    Console.WriteLine("{0} {1}", reader.GetString(1), reader.GetString(2));
                }
            }
            catch (Exception error)
            {
                Console.WriteLine(error.Message);
                throw new DataErrorException("FriendRelationData.Show:show的SqlCommand执行的时候出错啦！", error);
            }
            finally
            {
                SqlDataManager.CloseConnection(curCommand.Connection);
                Console.WriteLine("-------------------------------------");
            }
        }
    }
}